package com.lin.controller;

import com.lin.VO.ResultVO;
import com.lin.dataobject.Features;
import com.lin.enums.ResultEnum;
import com.lin.exception.SellException;
import com.lin.form.FeatureForm;
import com.lin.server.FeaturesService;
import com.lin.utils.ResultVOUtils;
import com.lin.utils.StringUtils;
import com.lin.utils.treenode.FeaturesTreeNode;
import com.lin.utils.treeuntil.FeatureTreeUntil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/22.
 */
@Controller
@RequestMapping("/feature")
public class SellerFeaturesController {
    @Autowired
    private FeaturesService featuresService;

    @RequestMapping("/treeView")
    public ModelAndView treeView(){
        return new ModelAndView("features/index1");
    }

//    @RequestMapping("/tree")
//    @ResponseBody
//    public List<FeaturesTreeNode> getTreeData(){
//        List<FeaturesTreeNode> list=null;
//        try{
//            //查询到所有的组织机构信息
//            List<Features> institutions = featuresService.findAll();
//            FeatureTreeUntil treeUntil=new FeatureTreeUntil();
//            //将查询到的数据转换为树形结构的list
//            list=treeUntil.fillTree(institutions);
//
//        }catch(Exception e) {
//            e.printStackTrace();
//        }
//        return list;
//    }

    @RequestMapping("/view")
    public ModelAndView view(@RequestParam(value = "id",required = false) String id,
                             Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            Features features= featuresService.findOne(Integer.valueOf(id));
            map.put("features",features);
        }
        return new ModelAndView("features/add_view");
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultVO save(@Valid FeatureForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        Features features=new Features();
        try{
            if (StringUtils.isNotEmpty(form.getId())){
                features= featuresService.findOne(form.getId());
            }
            if (!StringUtils.isNotEmpty(form.getPid())){
                form.setPid(0);
            }
            BeanUtils.copyProperties(form,features);
            featuresService.save(features);
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = false) Integer id){
        Features features= featuresService.findOne(id);
        if (features==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        Long childrenCount= featuresService.getChildrenCount(features.getId());
        if (childrenCount>0){
            return ResultVOUtils.error(ResultEnum.HAVE_CHILDREN.getCode(),
                    ResultEnum.HAVE_CHILDREN.getMessage());
        }
        featuresService.del(features);
        return ResultVOUtils.success("删除成功");
    }

    @RequestMapping("/getAllData")
    @ResponseBody
    public List<Features> getAllData(){
        List<Features> list=featuresService.findAll();
        return list;
    }
}

